<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class ShopService extends BaseService
{
    //门店

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbShop)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有门店');
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res, 'total' => $total);
    }

    /**
     * 一个
     * @param int $id 门店ID
     */
    public function getOne($id)
    {
        $res = (array) DB::table($this->dbShop)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有商品');
        }
        $arys = [
            'id' => $res['id'],
            'shop_name' => $res['shop_name'],
            'address' => $res['address'],
            'phone' => $res['phone'],
            'map' => $res['map'],
            'uptime' => $res['uptime'],
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 添加
     * @param array $pas【title 门店名称，$address 地址，map 坐标】
     */
    public function addOne($pas)
    {
        $resOne = (array) DB::table($this->dbShop)
            ->where('del', 0)
            ->where('shop_name', $pas['title'])
            ->where('address', $pas['address'])
            ->first();
        if (!empty($resOne)) {
            return array('code' => -2, 'msg' => '门店有重复');
        }
        $sData = [
            'shop_name' => $pas['title'],
            'address' => $pas['address'],
            'phone' => $pas['phone'],
            'map' => $pas['map'],
            'addtime' => toDate(),
            'uptime' => toDate(),
        ];
        $sid = DB::table($this->dbShop)->insertGetId($sData);
        if (!$sid) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code'=>0, 'msg'=>'success');
//        DB::beginTransaction(); //开启事务
//        try {
//            DB::commit(); //提交事务
//        } catch (\Exception $e) {
//            DB::rollBack(); //事务回滚
//            echo $e->getMessage();
//            return array('code'=>-1, 'msg'=>'操作失败');
//        }
    }

    /**
     * 修改
     * @param int $id 门店ID
     * @param array $pas【title 门店名称，$address 地址，map 坐标】
     */
    public function modify($id, $pas)
    {
        $res = (array) DB::table($this->dbShop)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '门店不存在');
        }
        $sData = [
            'shop_name' => $pas['title'],
            'address' => $pas['address'],
            'phone' => $pas['phone'],
            'map' => $pas['map'],
            'uptime' => toDate(),
        ];
        $sup = DB::table($this->dbShop)->where('id', $id)->update($sData);
        if (!$sup) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code'=>0, 'msg'=>'success');
    }

    /**
     * 删除
     * @param int $id 门店ID
     */
    public function setDel($id)
    {
        $res = (array) DB::table($this->dbShop)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '门店不存在');
        }
        $arys = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = DB::table($this->dbShop)->where('id', $id)->update($arys);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }
}